﻿using System;
using System.Collections;
using System.Data;
using visinia.SmartEngine.Cache;
using visinia.SmartEngine.Installer;
using visinia.SmartEngine.Pages;
using viWiki.Providers;

namespace viWiki.Controllers
{
    /// <summary>
    /// WikiController is the wiki main settings controller    
    /// </summary>
    public class WikiController
    {
        private const string KEY_WIKI_SETTINGS = "wikiSettings";

        /// <summary>
        /// The url separator for the viWiki plugin
        /// </summary>
        public static string WikiUrlSeparator
        {
            get { return PluginController.GetInstalledPlugin("viWiki").UrlSeparator; }
        }

        /// <summary>
        /// The webpage name of the wiki used internally
        /// </summary>
        public static string WikiWebPageName
        {
            get { return "viWikiWebPage"; }
        }
         
        /// <summary>
        /// The wiki webpage id used internally
        /// </summary>
        public static int WikiWebPageId
        {
            get 
            {
                if(Settings["viWikiWebPageId"] != null)
                    return Convert.ToInt32(Settings["viWikiWebPageId"]);
                PageInfo page = PageController.GetPage("viWikiWebPageId");
                if (page == null)
                {
                    Plugin plugin = PluginController.GetInstalledPlugin("viWiki");
                    int pageid = PageController.AddPage(new PageInfo(WikiWebPageName, "Wiki", plugin.PluginId));
                    AddSetting("viWikiWebPageId", pageid.ToString());
                    return pageid;
                }
                else
                {
                    AddSetting("viWikiWebPageId", page.PageId.ToString());
                    return page.PageId;
                }
            }
            set { AddSetting("viWikiWebPageId", value.ToString()); }
        }

        /// <summary>
        /// The wiki page id used as wiki front page
        /// </summary>
        public static int WikiMainPageId
        {
            get { return Convert.ToInt32(Settings["viWikiMainPageId"]); }
            set { AddSetting("viWikiMainPageId", value.ToString()); }
        }

        /// <summary>
        /// The wiki root page the parent of all wiki pages
        /// </summary>
        public static int WikiRootPageId
        {
            get { return Convert.ToInt32(Settings["viWikiRootPageId"]); }
            // don't uncomment otherwise generate all wiki page links 
            //set { AddSetting("viWikiRootPageId", value.ToString()); }
        }

        /// <summary>
        /// Gets the wiki setting
        /// </summary>
        /// <param name="key">The settings key to get</param>
        /// <returns>returns the setting value</returns>
        public static string GetSetting(string key)
        {
            if (Settings.Contains(key))
                return (string)Settings[key];
            else
                return string.Empty;
        }

        private static Hashtable _Settings;
        private static Hashtable Settings
        {
            get
            {
                _Settings = (Hashtable)DataCache.GetCache(KEY_WIKI_SETTINGS);
                if (_Settings == null)
                {
                    _Settings = new Hashtable();
                    IDataReader dr = viWikiDataProvider.Provider.GetWikiSettings();
                    while (dr.Read())
                    {
                        if (dr["SettingName"] != Convert.DBNull)
                        {
                            if (dr["SettingValue"] != Convert.DBNull)
                                _Settings[(string)dr["SettingName"]] = (string)dr["SettingValue"];
                            else
                                _Settings[(string)dr["SettingName"]] = "";
                        }
                    }
                    dr.Close();
                    DataCache.SetCache(KEY_WIKI_SETTINGS, _Settings);
                }
                return _Settings;
            }
        }

        #region DATA
        /// <summary>
        /// Adds a new wiki setting Or updates it if it already exists
        /// </summary>
        /// <param name="key">The key to setting</param>
        /// <param name="value">The value of the setting</param>
        public static void AddSetting(string key, string value)
        {
            if (Settings.ContainsKey(key))
                viWikiDataProvider.Provider.UpdateWikiSetting(key, value);
            else
                viWikiDataProvider.Provider.AddWikiSetting(key, value);
            DataCache.DeleteCacheMultiple("wiki");
        }
        /// <summary>
        /// Deletes the wiki setting
        /// </summary>
        /// <param name="key">The key to delete</param>
        public static void DeleteSetting(string key)
        {
            viWikiDataProvider.Provider.DeleteWikiSetting(key);
            DataCache.DeleteCacheMultiple("wiki");
        }
        #endregion
    }
}
